var navTab = {
	componentBox : null, // tab component. contain tabBox, prevBut, nextBut, panelBox
	_tabBox : null,
	_prevBut : null,
	_nextBut : null,
	_panelBox : null,
	_moreBut : null,
	_moreBox : null,
	_currentIndex : 0,

	_op : {
		id : "navTab",
		stTabBox : ".layui-tab-title",
		stPanelBox : ".layui-tab-content",
		mainTabId : "main",
		close$ : "a.close",
		prevClass : "tabsLeft",
		nextClass : "tabsRight",
		stMore : ".tabsMore",
		stMoreLi : "ul.tabsMoreList"
	},

	init : function(options) {
		if ($.History)
			$.History.init("#container");
		var $this = this;
		$.extend(this._op, options);

		this.componentBox = $("#" + this._op.id);
		this._tabBox = this.componentBox.find(this._op.stTabBox);
		this._panelBox = this.componentBox.find(this._op.stPanelBox);
		this._prevBut = this.componentBox.find("." + this._op.prevClass);
		this._nextBut = this.componentBox.find("." + this._op.nextClass);
		this._moreBut = this.componentBox.find(this._op.stMore);
		this._moreBox = this.componentBox.find(this._op.stMoreLi);

	},
	_getTabs : function() {
		return this._tabBox.find("> li");
	},
	_getPanels : function() {
		return this._panelBox.find("> div");
	},
	getPanel : function(tabid) {
		var index = this._indexTabId(tabid);
		if (index >= 0) {
			return this._getPanels().eq(index);
		}
	},
	_indexTabId : function(tabid) {
		if (!tabid)
			return -1;
		var iOpenIndex = -1;
		this._getTabs().each(function(index) {
			if ($('span', this).attr("tabid") == tabid) {
				iOpenIndex = index;
				return;
			}
		});
		return iOpenIndex;
	},
	openTab : function(tabid, title, url) {
		title = title + '<span tabid="{0}"></span>'.format(tabid);

		// loading层
		var loading = layer.load(2, {
			shade : [
					0.5, '#000'
			]
		});
		/***********************************************************************************************************************************************************************************************
		 * src加载方式
		 **********************************************************************************************************************************************************************************************/
		var index = navTab._indexTabId(tabid);
		var $panel;
		if (index >= 0) {
			$panel = navTab.getPanel(tabid);
			$panel.find('iframe').eq(0).attr('src', url);
		} else {
			index = navTab._indexTabId(tabid);
			$panel = navTab.getPanel(tabid);
		}

		$panel.find('iframe').get(0).onload = function() {
			layer.close(loading);
		};
		// $panel.find('iframe').height($panel.parent().height() - 40);
		/***********************************************************************************************************************************************************************************************
		 * ajax 加载方式
		 **********************************************************************************************************************************************************************************************/
		// $.get(url, function(data) {
		// var index = navTab._indexTabId(tabid);
		// console.log(index)
		// var $panel;
		// if (index >= 0) {
		// $panel = navTab.getPanel(tabid);
		// // $panel.find('iframe').html(data);
		// var doc = $('iframe', $panel)[0].contentWindow.document;
		// console.log($(doc.body).html())
		// } else {
		// index = navTab._indexTabId(tabid);
		// $panel = navTab.getPanel(tabid);
		// // $panel.find('iframe').html(data);
		//
		// console.log($('iframe', $panel).contents())
		// }
		// layer.close(loading);
		// });
	},
	_closeTab : function(index, openTabid) {
		var element = layui.element();
		element.tabDelete(this._op.id, index);
	},
	closeTab : function(tabid) {
		var index = this._indexTabId(tabid);
		if (index > 0) {
			this._closeTab(index);
		}
	},
	closeCurrentTab : function() {
		var tabid = $('span', this._tabBox.find(".layui-this")).attr("tabid");
		var index = this._indexTabId(tabid);
		this.closeTab(tabid);
	},
	getCurrentPanel : function() {
		var tabid = $('span', this._tabBox.find(".layui-this")).attr("tabid");
		var index = this._indexTabId(tabid);
		return this._getPanels().eq(index);
	}
}
$(function() {
	mainHeight = $(document.body).height() - 45;
	$('.main-left,.main-right').height(mainHeight);
	$("[addtabs]").click(function() {
		addTabs({
			id : $(this).attr("id"),
			title : $(this).attr('title'),
			close : true
		});
	});

	$(".nav-tabs").on("click", "[tabclose]", function(e) {
		id = $(this).attr("tabclose");
		closeTab(id);
	});
});
$.fn.extend({
	navTab : function() {
		return this.each(function() {
			var $this = $(this);
			$this.click(function(event) {
				var $this = $(this);
				var href = $this.attr("href");
				var tabid = $this.attr("tabid") || (href.indexOf('?') < 0 ? href : href.substring(0, href.indexOf('?')));
				var title = $this.attr('title') || $this.text();

				navTab.openTab('welcome', title, href);

				event.preventDefault();
			});
		});
	}
});